﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinkCard.Model;

namespace LinkCard.Dao
{
    public class CustomerDao: ITopLinkDao   
    {
        public int Add(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objAdd = (Customer)obj;
                db.AddToCustomers(objAdd);
                rs = db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Update(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objUpdate = (Customer)obj;
                var id = objUpdate.CustomerID;
                var oldObj = db.Customers.FirstOrDefault(t => t.CustomerID == id);
                if (oldObj != null)
                {
                    oldObj.Fullname = objUpdate.Fullname;
                    oldObj.CardNumber = objUpdate.CardNumber;
                    oldObj.Birthday = objUpdate.Birthday;
                    oldObj.ID= objUpdate.ID;
                    oldObj.Gender = objUpdate.Gender;
                    oldObj.MobilePhone = objUpdate.MobilePhone;
                    oldObj.Email = objUpdate.Email;
                    oldObj.Address = objUpdate.Address;
                    oldObj.Job = objUpdate.Job;
                    oldObj.Income = objUpdate.Income;
                    oldObj.OtherInfo = objUpdate.OtherInfo;
                    oldObj.TransactionAmount = objUpdate.TransactionAmount;
                    oldObj.SystemRegister = objUpdate.SystemRegister;
                    oldObj.Point = objUpdate.Point;
                    oldObj.IsActive = objUpdate.IsActive;
                    oldObj.GUID = objUpdate.GUID;
                    oldObj.GUIDCreateDate = objUpdate.GUIDCreateDate;
                    oldObj.Status = objUpdate.Status;
                    oldObj.Version = objUpdate.Version;
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Delete(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objDel = (Customer)obj;
                var id = objDel.CustomerID;
                var oldObj = db.Customers.FirstOrDefault(t => t.CustomerID == id);
                if (oldObj != null)
                {
                    db.DeleteObject(oldObj);
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public Customer GetById(long id)
        {
            var ct = new Customer();
            try
            {
                var db = new TopLinkEntities();
                ct = db.Customers.FirstOrDefault(t => t.CustomerID == id);
            }
            catch (Exception)
            {
                throw;
            }
            return ct;
        }

        public List<Customer> GetAll()
        {
            var lst = new List<Customer>();
            try
            {
                var db = new TopLinkEntities();
                lst = db.Customers.ToList();
            }
            catch (Exception)
            {
                throw;
            }
            return lst;
        }
    }
}
